﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnalysisTesteur.Views
{
    /// <summary>
    /// Logique d'interaction pour GridSheetItemControl.xaml
    /// </summary>
    public partial class GridSheetItemControl : UserControl
    {
        public GridSheetItemControl()
        {
            InitializeComponent();
        }

        private void ContainerGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var colsizes = ContainerGrid.ColumnDefinitions.Select((coldef) => coldef.Width).ToArray();

            ContainerGrid.Children.Clear();
            ContainerGrid.ColumnDefinitions.Clear();
            ContainerGrid.RowDefinitions.Clear();


            var tableitem = this.DataContext as Models.DashboardItemImpls.GridSheetItem;
            if (tableitem == null) return;
            var rowset = tableitem.TabularSet;
            if (rowset == null) return;

            var rowdimdic = tableitem.RowDimensionList.ToDictionary((d) => d.SheetItemDimension.UniqueID);
            var coldimdic = tableitem.ColumnDimensionList.ToDictionary((d) => d.SheetItemDimension.UniqueID);

            var mdic = tableitem.MeasureList.ToDictionary((m) => m.SheetItemMeasure.UniqueID);
            for (int i = 0; i < rowset.RowDimensionIdList.Length; i++)
            {
                ContainerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            }
            for (int i = 0; i < rowset.ColumnDimensionIdList.Length; i++)
            {
                ContainerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            }

            var startrow = rowset.ColumnDimensionIdList.Length;
            var startcol = rowset.RowDimensionIdList.Length;

            for (int i = 0; i < rowset.RowAxisItemList.Length; i++)
            {
                ContainerGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                for (int j = 0; j < rowset.RowAxisItemList[i].DimensionItems.Length; j++)
                {
                    ContainerGrid.Children.Add(
                        CreateCell(startrow + i, j, 1, 1, rowset.RowAxisItemList[i].DimensionItems[j].Value));
                }
            }
            for (int i = 0; i < rowset.ColumnAxisItemList.Length; i++)
            {
                ContainerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto, MinWidth=25 });
                for (int j = 0; j < rowset.ColumnAxisItemList[i].DimensionItems.Length; j++)
                {
                    ContainerGrid.Children.Add(
                        CreateCell(j, startcol + i, 1, 1, rowset.ColumnAxisItemList[i].DimensionItems[j].Value));
                }
            }
            if (rowset.MeasureIdList.Length > 0)
            {
                foreach (var item in rowset.CellList)
                {
                    ContainerGrid.Children.Add(
                        CreateCell(startrow + item.RowPositon, startcol + +item.ColumnPositon, 1, 1, item.MeasureValues[0], true));
                }
            }

            ContainerGrid.RowDefinitions.Add(new RowDefinition());
            ContainerGrid.ColumnDefinitions.Add(new ColumnDefinition());

            for (int i = 0; i < ContainerGrid.RowDefinitions.Count-1; i++)
            {
                CreateRowLine(i);
            }
            for (int i = 0; i < ContainerGrid.ColumnDefinitions.Count-1; i++)
            {
                CreateColumnLine(i);
            }

            for (int i = 0; i < ContainerGrid.ColumnDefinitions.Count - 1; i++)
            {
                var sp = new GridSplitter();
                sp.Width = 2;
                sp.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                sp.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                Grid.SetColumn(sp, i);
                ContainerGrid.Children.Add(sp);
            }
            for (int i = 0; i < ContainerGrid.ColumnDefinitions.Count - 1; i++)
            {
                if (i < colsizes.Length-1 )
                {
                    ContainerGrid.ColumnDefinitions[i].Width = colsizes[i];
                }
            }
        }

        private void CreateRowLine(int row)
        {
            var result = new Rectangle();
            result.Fill = Brushes.Black;
            result.Height = 1;
            Grid.SetColumn(result, 0);
            Grid.SetRow(result, row);
            Grid.SetColumnSpan(result, this.ContainerGrid.ColumnDefinitions.Count);
            Grid.SetRowSpan(result, 1);
            result.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            result.Focusable = false;
            result.IsHitTestVisible = false;
            this.ContainerGrid.Children.Add(result);
        }
        private void CreateColumnLine(int colum)
        {
            var result = new Rectangle();
            result.Fill = Brushes.Black;
            result.Width = 1;
            Grid.SetColumn(result, colum);
            Grid.SetRow(result, 0);
            Grid.SetColumnSpan(result, 1);
            Grid.SetRowSpan(result,  this.ContainerGrid.RowDefinitions.Count);
            result.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            result.Focusable = false;
            result.IsHitTestVisible = false;
            this.ContainerGrid.Children.Add(result);
        }

        private UIElement CreateCell(int row, int colum, int rowspan, int columnspan, object value , bool isdata= false)
        {
            var result = new TextBlock();
            Grid.SetColumn(result, colum);
            Grid.SetRow(result, row);
            Grid.SetColumnSpan(result, columnspan);
            Grid.SetRowSpan(result, rowspan);
            //result.Content = value == null ? null : (value is double?) ? ((double?)value).Value.ToString("N0") : value;
            result.Margin = new Thickness(2);
            result.Padding = new Thickness(2);
            result.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            //result.MinWidth = 25;
            //result.MinHeight = 25;
            //if (value is double?) result.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
            if (value != null)
            {
                result.Text =isdata ? ((double?)value).Value.ToString("N0") : value.ToString() ;
                result.TextAlignment = isdata ? TextAlignment.Right : TextAlignment.Left;
            }

            return result;
        }
    }
}
